package com.example.happiness.controller.admin;

import com.example.happiness.constant.Message;
import com.example.happiness.pojo.generalDTO.OrderStateDTO;
import com.example.happiness.pojo.admindto.OrdersPageDto;
import com.example.happiness.pojo.adminvo.PageResult;
import com.example.happiness.result.Result;
import com.example.happiness.service.admin.AdminOrderDispatchService;
import com.example.happiness.service.admin.AdminOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Api(tags = "订单相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/order")
public class AdminOrderController {

    @Autowired
    private AdminOrderService adminOrderService;
    @Autowired
    AdminOrderDispatchService adminOrderDispatchService;

    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> getOrderList(OrdersPageDto ordersPageDto){
        log.info("分页查询：{}", ordersPageDto);
        PageResult pageResult = adminOrderService.pageQuery(ordersPageDto);
        return Result.success(pageResult);
    }

    @PutMapping("/updataorderstate")
    @ApiOperation("修改订单状态")
    public Result updataOrderState(@RequestBody OrderStateDTO orderStateDTO){
        if(Objects.equals(orderStateDTO.getState(), Message.THREE)){
            orderStateDTO.setProportion((float) (orderStateDTO.getProportion()*0.01));
            adminOrderDispatchService.addOrderDispatch(orderStateDTO);
        }
        log.info("修改订单状态：{}", orderStateDTO.toString());
        adminOrderService.updataOrderState(orderStateDTO);
        return Result.success();
    }
}
